#' A PDF format for documents based on the LaTeX package \pkg{k2tex}
#'
#' \code{k2tex()} is a wrapper function for \code{rmarkdown::pdf_document()} and
#' changed the default values of two arguments \code{template} and
#' \code{latex_engine} so it works better with the \pkg{k2tex} package.
#' @param ...,template,latex_engine Passed to
#'   \code{markdown::\link[rmarkdown]{pdf_document}()}
#' @return \code{k2tex()} returns a format that can be passed to
#'   \code{rmarkdown::render()}; \code{k2tex_template()} returns the path to a
#'   LaTeX template in \pkg{k2doc} for Chinese documents using the \pkg{k2tex}
#'   package.
#'
#' @examples
#'
#' \dontrun{
#' library(rmarkdown)
#' draft("MyArticle.Rmd", template = "k2tex", package = "k2doc")
#' }
#'
#' @export
k2tex <- function(..., template = k2tex_template(), latex_engine = 'xelatex', logo=k2tex_logo(),
                  first_background=k2tex_background()) {
  fmt <- inherit_pdf_document(..., template = template, latex_engine = latex_engine)
  pre <- fmt$pre_processor
  fmt$pre_processor <- function(...) {
    c(if (is.function(pre)) pre(...), '--variable', paste0('logo=', logo),
      '--variable', paste0('first_background=', first_background))
  }
  fmt
}


#' @rdname k2tex
#' @export
k2tex_template <- function() find_resource('k2tex', 'default.latex')

k2tex_logo <- function() find_resource('k2tex',  'k2data.png')

k2tex_background <- function() find_resource('k2tex', 'mountain_bg.png')
